import time

from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt

from .models import MusicFavorite, PostFavorite
from music.models import MusicList
from post.models import PostList
from user.models import User
from trend.models import TrendList
# Create your views here.

@csrf_exempt
@login_required
def add_favorite_music(request):
    if request.method == 'POST':
        music_id = request.POST.get('music_id', '')
        user_id = request.POST.get('user_id', '')

        user = User.objects.get(id=user_id)
        music = MusicList.objects.get(id=music_id)
        collect_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

        favorite = MusicFavorite.objects.filter(user=user, music=music)
        if favorite:
            # 如果查找到数据库中有该条记录,则从数据库中移除,并增加该条动态
            return JsonResponse({'status': 'warning', 'msg': '该音乐已经在收藏夹了哦'})
        else:
            MusicFavorite.objects.get_or_create(user=user, music=music, collect_time=collect_time)
            trend_content = '将音乐 《%s》 加入了收藏夹' % music.name
            trend = TrendList.objects.create(user=user, content=trend_content)
            trend.save()
            return JsonResponse({'status': 'ok', 'msg': '收藏成功', 'id': music_id})


@csrf_exempt
@login_required
def add_favorite_post(request):
    if request.method == 'POST':
        post_id = request.POST.get('post_id', '')
        user_id = request.POST.get('user_id', '')

        user = User.objects.get(id=user_id)
        post = PostList.objects.get(id=post_id)
        collect_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

        favorite = PostFavorite.objects.filter(user=user, post=post)
        if favorite:
            return JsonResponse({'status': 'warning', 'msg': '该主题已经在收藏夹了哦'})
        else:
            PostFavorite.objects.get_or_create(user=user, post=post, collect_time=collect_time)
            trend_content = '将主题 《%s》 加入了收藏夹' % post.title
            trend = TrendList.objects.create(user=user, content=trend_content)
            trend.save()
            return JsonResponse({'status': 'ok', 'msg': '收藏成功'})


@login_required
def delete_favorite_music(request, pk):
    redirect_to = request.GET.get('next', '')

    if request.method == 'GET':
        f_music = MusicFavorite.objects.filter(pk=pk)

        if f_music:
            trend_content = '从收藏夹移除音乐 《%s》' % f_music[0].music.name
            f_music.delete()
            trend = TrendList.objects.create(user=request.user, content=trend_content)
            trend.save()

        if redirect_to:
            return redirect(redirect_to)

    trend_list = TrendList.objects.filter(user=request.user).order_by('-trend_time')
    f_music_list = MusicFavorite.objects.filter(user=request.user)
    f_post_list = PostFavorite.objects.filter(user=request.user)

    return render(request, 'user/profile.html', context={'trend_list': trend_list,
                                                         'f_music_list': f_music_list,
                                                         'f_post_list': f_post_list})


@login_required
def delete_favorite_post(request, pk):
    redirect_to = request.GET.get('next', '')

    if request.method == 'GET':
        f_post = PostFavorite.objects.filter(pk=pk)

        if f_post:
            trend_content = '从收藏夹移除主题 《%s》 ' % f_post[0].post.title
            f_post.delete()
            trend = TrendList.objects.create(user=request.user, content=trend_content)
            trend.save()

        if redirect_to:
            return redirect(redirect_to)

    trend_list = TrendList.objects.filter(user=request.user).order_by('-trend_time')
    f_music_list = MusicFavorite.objects.filter(user=request.user)
    f_post_list = PostFavorite.objects.filter(user=request.user)

    return render(request, 'user/profile.html', context={'trend_list': trend_list,
                                                         'f_music_list': f_music_list,
                                                         'f_post_list': f_post_list})
